/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ConfigDialog.java
 *
 * Created on 30.12.2008, 01:40:43
 */

package de.blitzcoder.collide.config;

import de.blitzcoder.collide.Config;
import de.blitzcoder.collide.Interface;
import de.blitzcoder.collide.config.textarea.TextAreaTab;
import de.blitzcoder.collide.engine.RunningProcess;
import de.blitzcoder.collide.gui.CodeDocument;
import de.blitzcoder.collide.gui.Document;
import de.blitzcoder.collide.gui.IDEDialog;
import java.awt.Component;
import java.awt.Dimension;

/**
 *
 * @author blitzcoder
 */
public class ConfigDialog extends IDEDialog {

    /** Creates new form ConfigDialog */
    public ConfigDialog() {
        super(Interface.get(),"Einstellungen");

        initComponents();
        setPreferredSize(new Dimension(500,550));
        pack();

        addTab(new EditorTab());
        addTab(new TextAreaTab());
        addTab(new MiscTab());

    }

    private void addTab(ConfigTab tab) {
        tab.init();
        tabbedPane.addTab(tab.getTitle(), tab);
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        tabbedPane = new javax.swing.JTabbedPane();
        okButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle(""); // NOI18N
        setModal(true);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });
        getContentPane().setLayout(new java.awt.GridBagLayout());
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        getContentPane().add(tabbedPane, gridBagConstraints);

        okButton.setText("Ok");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(okButton, gridBagConstraints);

        cancelButton.setText("Abbrechen");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(cancelButton, gridBagConstraints);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
        saveAndDispose();
    }//GEN-LAST:event_okButtonActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        cancelAndDispose();
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        //save();
    }//GEN-LAST:event_formWindowClosed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        saveAndDispose();
    }//GEN-LAST:event_formWindowClosing

    private void save() {
        Component[] comps = tabbedPane.getComponents();
        for (Component c : comps) {
            if (c instanceof ConfigTab) {
                ConfigTab tab = (ConfigTab)c;
                tab.save();
            }
        }
    }

    private void saveAndDispose() {
        save();
        dispose();
    }

    private void cancelAndDispose() {
        Component[] comps = tabbedPane.getComponents();
        for (Component c : comps) {
            if (c instanceof ConfigTab) {
                ConfigTab tab = (ConfigTab)c;
                tab.cancel();
            }
        }
        dispose();
    }

    @Override
    public void dispose() {
        super.dispose();
        updateComponents();
    }

    private void updateComponents() {
        Document[] docs = Interface.getDocumentManager().getAllDocuments();
        for (Document doc:docs) {
            if (doc instanceof CodeDocument)
                ((CodeDocument)doc).getTextArea().propertiesChanged();
        }
        docs = Interface.getOutput().getAllDocuments();
        for (Document doc:docs) {
            if (doc instanceof RunningProcess) {
                RunningProcess rp = (RunningProcess)doc;
                rp.setTextAreaFont(Config.getFontProperty("output.font"));
            }
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JButton okButton;
    private javax.swing.JTabbedPane tabbedPane;
    // End of variables declaration//GEN-END:variables

}
